package cn.com.chengmandian.bike.controller;


import cn.com.chengmandian.agent.utils.AuthUtil;
import cn.com.chengmandian.bike.dto.RideCloseCheckForm;
import cn.com.chengmandian.bike.model.EbMemberInfo;
import cn.com.chengmandian.bike.model.EbRideClose;
import cn.com.chengmandian.bike.model.EbRideOrder;
import cn.com.chengmandian.bike.model.vo.OrderCloseCheckVo;
import cn.com.chengmandian.bike.service.IEbMemberInfoService;
import cn.com.chengmandian.bike.service.IEbRideCloseService;
import cn.com.chengmandian.bike.service.IEbRideOrderService;
import cn.com.chengmandian.core.common.constants.CommonConstant;
import cn.com.chengmandian.core.redis.cache.AgentParamCacheService;
import cn.com.chengmandian.core.web.exceptions.BadResponseException;
import cn.com.chengmandian.core.web.pojo.AgentUser;
import cn.com.chengmandian.core.web.pojo.AjaxResult;
import cn.com.chengmandian.core.web.pojo.BikeAreaParam;
import cn.com.chengmandian.service.bike.constants.RideOrderStatusEnum;
import cn.com.chengmandian.service.bike.constants.UserTypeEnum;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * <p>
 * 电单车运营-订单停车关锁行为记录 前端控制器
 * </p>
 *
 * @author AutoGenerator
 * @since 2024-10-26
 */
@Api(tags = "订单停车关锁行为记录")
@RestController
@RequestMapping("/ride/close")
public class EbRideCloseController {

    @Autowired
    private IEbRideCloseService rideCloseService;

    @Autowired
    private IEbRideOrderService rideOrderService;

    @Autowired
    private AgentParamCacheService agentParamCacheService;

    @Autowired
    private IEbMemberInfoService memberInfoService;

    @ApiOperation("停车行为检查")
    @PostMapping("/check")
    public AjaxResult<OrderCloseCheckVo> closeCheck(@RequestBody RideCloseCheckForm form) {
        Assert.isTrue(StrUtil.isNotBlank(form.getOrderNo()), "缺少订单号");
        AgentUser agentUser = AuthUtil.getAgentUser();
        EbRideOrder rideOrder = rideOrderService.getByNo(form.getOrderNo(), agentUser.getId());
        Assert.notNull(rideOrder, "订单不存在");
        Assert.isTrue(rideOrder.getStatus().equals(RideOrderStatusEnum.RIDING.getCode())
                || rideOrder.getStatus().equals(RideOrderStatusEnum.PAUSE.getCode()), "订单已结束");
        EbRideClose rideClose = new EbRideClose();
        rideClose.setPortParams(JSONObject.toJSONString(form));
        Date portStartTime = new Date();
        rideClose.setPortStartTime(portStartTime);
        try {
            EbMemberInfo memberInfo = memberInfoService.getById(agentUser.getId());
            Integer memberId = memberInfo.getId();
            String memberName = StrUtil.isBlank(memberInfo.getName())? memberInfo.getMobile() : memberInfo.getName();
            rideCloseService.closeCheck(rideClose, rideOrder, form.getIsDispatch(), form.getPayHelmetMoney(), form.getAiPhotosPass(),
                    true, UserTypeEnum.MEMBER.getCode(), memberId, memberName, form.getMemberLng(), form.getMemberLat());
        }catch (BadResponseException ex) {
            rideClose.setPortReturnResult(this.getPortReturnResult(ex.getCode(), ex.getMessage(), ex.getData()));
            Date portEndTime = new Date();
            rideClose.setPortEndTime(portEndTime);
            rideClose.setPortTakeTime((int)DateUtil.between(portStartTime, portEndTime, DateUnit.MS, true));
            rideCloseService.save(rideClose);
            throw new BadResponseException(ex.getCode(), ex.getMessage(), ex.getData());
        }catch (Exception ex) {
            rideClose.setPortReturnResult(this.getPortReturnResult(500, ex.getMessage(), null));
            Date portEndTime = new Date();
            rideClose.setPortEndTime(portEndTime);
            rideClose.setPortTakeTime((int)DateUtil.between(portStartTime, portEndTime, DateUnit.MS, true));
            rideCloseService.save(rideClose);
            throw new BadResponseException(ex.getMessage());
        }

        BikeAreaParam bikeAreaParam = agentParamCacheService.getParam(rideOrder.getAgentId(), rideOrder.getAreaId());
        OrderCloseCheckVo vo = new OrderCloseCheckVo(bikeAreaParam == null? CommonConstant.ONE : bikeAreaParam.getUserRideReturnBikeProcess());
        rideClose.setPortReturnResult(this.getPortReturnResult(200, "检查通过", vo));
        Date portEndTime = new Date();
        rideClose.setPortEndTime(portEndTime);
        rideClose.setPortTakeTime((int)DateUtil.between(portStartTime, portEndTime, DateUnit.MS, true));
        rideCloseService.save(rideClose);
        return AjaxResult.success("检查通过", vo);
    }

    private String getPortReturnResult(Integer code, String message, Object data) {
        JSONObject portReturnResult = new JSONObject();
        portReturnResult.put("code", code);
        portReturnResult.put("data", data);
        portReturnResult.put("message", message);
        return portReturnResult.toJSONString();
    }
}
